﻿<!doctype html>
<html>
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
    <title>部门信息</title>
    <script src="/webjars/miniui/boot.js" type="text/javascript"></script>
    <script src="/static/js/common/common.js" type="text/javascript"></script>
    <link href="/static/css/resource.css" rel="stylesheet" type="text/css" />
    <style type="text/css">
        body {
            margin:0;
            padding:0;
            border:0;
            width:100%;
            height:100%;
            overflow:hidden;
        }
    </style>
</head>
<body>
<div class="mini-splitter" style="width:100%;height:100%;" borderStyle="border:8px;">
    <!-- 左边树 -->
    <div size="310" showCollapseButton="true">
        <div class="mini-fit" style="margin-top: 10px;margin-left: 10px">
            <!--Tree -->
            <ul id="tree1" class="mini-tree" url="/basic/departments/loadTree"
                showTreeIcon="true"
                ShowExpandButtons="true"
                resultAsTree="false" expandOnLoad='false'
                textField="name" idField="id" parentField="pid"
                contextMenu="#treeMenu"
                onbeforeload="onBeforeTreeLoad"
                expandOnNodeClick="false"
            >
            </ul>
        </div>
    </div>
    <div showCollapseButton="true">
        <div id="top" class="mini-toolbar" style=" padding:2px; background-color: #9cdbf7; " hidden="hidden">
            <a class="mini-button" iconCls="fa-save" plain="true" onclick="saveData()">保存</a>
            <span class="separator"></span>
        </div>
        <div class="mini-fit">
            <!-- 资源 编辑框START-->
            <div id="editform" class="form" style="margin-left: 15px;margin-top: 8px" hidden="hidden">
                <input name="id" class="mini-hidden" style="width: 100%;" />
                <table class="form-table" border="0" cellspacing="8"
                       style="width: 80%; table-layout: fixed;">
                    <tr>
                        <td style="width: 10%;" align="right"><font color="#dc143c">*</font>部门名称:</td>
                        <td style="width: 30%">
                            <input name="name" class="mini-textbox" style="width: 100%;" required="true" />
                        </td>
                    </tr>
                    <tr>
                        <td style="width: 10%;" align="right"><font color="#dc143c">*</font>机构类型:</td>
                        <td style="width: 30%">
                            <input name="level" id="level" class="mini-combobox" style="width:100%;"
                                    <#--data="[{ id: '1', name: '司法部' },{ id: '2', name: '司法部监狱局' },{ id: '01', name: '监狱' },{ id: '02', name: '看守所' },{ id: '03', name: '司法所' },
                                    { id: '04', name: '未管所' },{ id: '05', name: '社区矫正' },{ id: '06', name: '司法厅' },
                                    { id: '07', name: '地市司法局' },{ id: '08', name: '区县司法局' },{ id: '09', name: '监狱局' },{ id: '10', name: '司法部法治局' },
                                    { id: '11', name: '教育矫治局'}]"-->
                                    <#--onclick="getLevel()"-->
                                   textField="name" valueField="id" emptyText="请选择..." required="true" />
                        </td>
                    </tr>
                    <tr>
                        <td style="width: 10%;" align="right"><font color="#dc143c">*</font>上级部门:</td>
                        <td style="width: 30%">
                            <input id="pname1" name="pid" class="mini-combobox" style="width:100%;"
                                   textField="name" valueField="id" emptyText="请选择..." required="true" />
                        </td>
                    </tr>
                    <tr>

                        <td style="width: 10%;" align="right"><font color="#dc143c">*</font>是否可用:</td>
                        <td style="width: 30%">
                            <input name="stat" class="mini-radiobuttonlist" style="width:100%;"
                                   data="[{ id: -1, name: '禁用' },{ id: 1, name: '启用' }]"
                                   textField="name" valueField="id" value='-1' required="true" />
                        </td>
                    </tr>
                </table>
            </div>
            <!-- 资源 编辑框END-->
        </div>
    </div>
</div>
<!-- 右键菜单项START -->
<ul id="treeMenu" class="mini-contextmenu" onbeforeopen="onBeforeOpen">
    <li name="add" iconCls="fa fa-plus" onclick="onAddNode">插入节点</li>
    <li name="remove" iconCls="fa fa-trash" onclick="onRemoveNode">删除节点</li>
</ul>
<!-- 右键菜单项END -->
<script type="text/javascript">
    mini.parse();
    var tree = mini.get("tree1");
    var pname1 = mini.get("pname1");
    var form = new mini.Form("#editform");
    var departId = mini.get("id");
    var levelEle = mini.get("level");

    function onBeforeTreeLoad(e) {
        var tree = e.sender;    //树控件
        var node = e.node;      //当前节点
        var params = e.params;  //参数对象
    }

    tree.on("nodeSelect", function (e) {
        var top = document.getElementById("top");
        top.style.display = 'block';
        var editform = document.getElementById("editform");
        editform.style.display = 'block';
        var row = tree.getSelectedNode();
        var o = mini.decode(row);
        if (o.id == null || o.id == "") {
            var jsonData = [{'id': '1', 'id': o.pid, 'name': o.pname}];
            getLevel(o.pid);
            pname1.load(jsonData);
            form.setData(o);
            form.setChanged(false);
        } else {
            $.ajax({
                url: "/basic/departments/getDepartInfo",
                type: "post",
                data: {id: o.id},
                success: function (text) {
                    var array = eval(text);
                    var newDepart = mini.decode(array[0]);
                    o.pid = newDepart.pid;
                    o.name = newDepart.name;
                    o.level = newDepart.level;
                    o.phone = newDepart.phone;
                    o.city = newDepart.city;
                    o.pid = newDepart.pid;
                    o.pname = newDepart.pname;
                    var jsonData = [{'id': '1', 'id': o.pid, 'name': o.pname}];
                    var jsonDataLevel = [{'id': '1', 'id': newDepart.level, 'name': newDepart.levelName}];
                    pname1.load(jsonData);
                    levelEle.load(jsonDataLevel);
                    form.setData(o);
                    form.setChanged(false);

                },
                error: function (jqXHR, textStatus, errorThrown) {
                    mini.alert("没有相应资源");
                }
            });
        }
    });

    //获取下级机构类型下拉
    function getLevel(pid) {
        var deptId = null;
        if (pid == null || pid == "") {
            deptId = departId.getValue();
        } else {
            deptId = pid;
        }
        $.ajax({
            url: "/basic/department/getLevelCombox?deptId=" + deptId,
            type: "post",
            dataType: "json",
            success: function (success) {
                var successCode = success.code;
                if (successCode == "1") {
                    var data = success.data;
                    levelEle.setData(data);
                } else {
                    mini.alert("请重试");
                }
            }
        });
    }

    //保存节点
    function saveData() {
        form.validate();
        if (form.isValid() == false) {
            return;
        }
        var data = form.getData();
        var json = mini.encode(data);
        commonSaveData("/basic/department", {data: json}, callback);

        // 保存成功的回调函数
        function callback() {
            tree.load("/basic/departments/loadTree");
            document.getElementById("editform").style.display = 'none';
            mini.alert("保存成功");
        }
    }

    //打开资源树前操作
    function onBeforeOpen(e) {
        var menu = e.sender;
        var node = tree.getSelectedNode();
        if (!node) {
            e.cancel = true;
            return;
        }
        if (node && node.text == "Base") {
            e.cancel = true;
            //阻止浏览器默认右键菜单
            e.htmlEvent.preventDefault();

        }
    }

    //添加节点
    function onAddNode(e) {
        var node = tree.getSelectedNode();
        var level = node.level;
        //05社矫中心、14区县社矫、11地市社矫、15省社矫机构
        if (level == "05" || level == "14" || level == "11" || level == "15") {
            mini.alert("社矫账号不能添加,添加司法所部门会默认添加社矫部门");
            return;
        }
        var newNode = {
            pid: node.id,
            orgid: node.orgid,
            orgname: node.orgname,
            pname: node.name,
        };
        tree.addNode(newNode, 0, node);
        tree.expandNode(node);
        tree.selectNode(newNode);
        // 设置数据
        form.setData(newNode, true);
    }

    //删除节点
    function onRemoveNode(e) {
        var node = tree.getSelectedNode();
        if (typeof (node.id) == "undefined") {
            tree.removeNode(node);
            tree.selectNode(tree.getParentNode(node));
            return;
        }
        var childs = tree.getAllChildNodes(node);
        var ids = []
        for (i = 0, len = childs.length; i < len; i++) {
            ids.push(childs[i].id);
        }
        ids.push(node.id);
        if (node) {
            commonRemove("/basic/department/delete", node, {_method: "delete", ids: ids, id: node.id}, callback);
        } else {
            mini.alert("请至少选中一条记录");
        }

        function callback() {
            tree.removeNode(node);
            tree.selectNode(tree.getParentNode(node));
        }
    }
</script>
</body>
</html>